package com.nd.mobile.study.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.constants.FlagConstants;
import com.nd.mobile.study.common.util.StringUtils;
import com.nd.mobile.study.domain.User;
import com.nd.mobile.study.view.helper.ToastHelper;
import com.nd.mobile.study.view.helper.Validator;

/**
 * 登录界面
 * 
 */
public class LoginActivity extends BaseActivity {

	static String TAG = "LoginActivity";

	private EditText mEdtUsername = null;

	private EditText mEdtPassword = null;

	private RelativeLayout rlLoginInput = null;

	private Button mBtnHeaderRegister = null;

	private Button mBtnLogin = null;

	private Button mBtnRegister = null;

	private TextView mTvErrorMessage = null;

	private int existCount = 0;

	private String username = null;

	private String password = null;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.login_input);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceState) {
		Intent intent = this.getIntent();
		// 获取标志位的数据
		String errorMsg = intent
				.getStringExtra(FlagConstants.LOGIN_FLAG_LOGIN_ERROR) == null ? ""
				: intent.getStringExtra(FlagConstants.LOGIN_FLAG_LOGIN_ERROR);
		boolean fromLogout = intent.getBooleanExtra(FlagConstants.LOGIN_FLAG_FROM_MORE,false);
		if (StringUtils.isBlank(errorMsg) == false) {
			ToastHelper.displayToast(this, errorMsg);
		} else if(fromLogout){
			ToastHelper.displayToast(this, "注销成功，请重新登录");
		} else {
			// 1.读取配置文件中的数据，并将配置文件中的数据显示于界面
			readAndShow();
		}
	}

	/**
	 * 控件初始化，事件初始化
	 */
	@Override
	protected void initViews() {
		this.initHeader();

		//控件初始化
		rlLoginInput = (RelativeLayout) this.findViewById(R.id.login_input);

		mEdtUsername = (EditText) findViewById(R.id.login_edt_username);
		mEdtPassword = (EditText) findViewById(R.id.login_edt_password);
		mBtnLogin = (Button) findViewById(R.id.login_btn_login);

		// 登陆按钮事件
		mBtnLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				login();
			}
		});

	}

	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderBtnLogin.setVisibility(View.GONE);
		mHeaderTitleTv.setText(R.string.btn_txt_login);

		mBtnHeaderRegister = (Button) findViewById(R.id.header_btn_register);
		mBtnHeaderRegister.setVisibility(View.VISIBLE);

		mBtnHeaderRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, RegisterActivity.class);
				startActivity(intent);
			}
		});
	}

	/**
	 * 读取登陆界面的数据，并显示
	 */
	private void readAndShow() {
		// 1.获取数据
		String[] loginUserInfo = User.getUserInfoFromSharedPreferences(mApp);
		username = loginUserInfo[0];
		password = loginUserInfo[1];
		// 2.显示数据
		mEdtUsername.setText(username);
		mEdtPassword.setText(password);

		mEdtUsername.setText("pgy123");
		mEdtPassword.setText("123456");
	}

	/**
	 * 登陆事件
	 */
	public void login() {
		// 获取用户名和密码
		this.username = this.mEdtUsername.getText().toString().trim();
		this.password = this.mEdtPassword.getText().toString().trim();

		// 检查用户名和密码
		if (!isValid())
			return;

		// 执行登录
		Intent intent = new Intent();
		intent.setClass(this, LoginLoadingActivity.class);
		intent.putExtra(BundleConstants.LOGIN_USERNAME, this.username);
		intent.putExtra(BundleConstants.LOGIN_PASSWORD, this.password);
		startActivity(intent);
	}

	private boolean isValid() {
		Validator validator = new Validator(this);
		return validator.validateUserName(mEdtUsername)
				&& validator.validatePassword(mEdtPassword);
	}

}
